<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>流程定义</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
    <link rel="stylesheet" href="../../../admin/css/reset.css"/>
</head>
<body class="pear-container">

<!-- 顶部查询表单 -->
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form top-search-from">
            <div class="layui-form-item">
                <label class="layui-form-label">唯一编码</label>
                <div class="layui-input-block">
                    <input type="text" name="name" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">显示名称</label>
                <div class="layui-input-block">
                    <input type="text" name="display_name" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <select name="state">
                        <option value="" selected>所有</option>
                        <option value="1">启用</option>
                        <option value="0">禁用</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item layui-inline">
                <label class="layui-form-label"></label>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                    <i class="layui-icon layui-icon-search"></i>查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                    <i class="layui-icon layui-icon-refresh"></i>重置
                </button>
            </div>
            <div class="toggle-btn">
                <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
            </div>
        </form>
    </div>
</div>


<!-- 数据表格 -->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="data-table" lay-filter="data-table"></table>
    </div>
</div>


<!-- 表格顶部工具栏 -->
<script type="text/html" id="table-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="wf-definition-add">
        <i class="layui-icon layui-icon-add-1"></i>导入
    </button>
</script>


<!-- 表格行工具栏 -->
<script type="text/html" id="table-bar">
    <button class="pear-btn pear-btn-xs tool-btn" lay-event="check" permission="wf-definition-check">查看</button>
    <button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="wf-definition-edit">修改</button>
    <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="wf-definition-delete">删除</button>
</script>

<!--状态开关-->
<script type="text/html" id="status">
    <input type="checkbox" name="state" value="{{d.id}}" permission="wf-process-status" lay-skin="switch" lay-text="" lay-filter="process-status" {{ d.state=== 1 ? 'checked' : '' }}/>
</script>


<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script src="../../../lms/base64.js"></script>
<script src="../../../lms/common.js"></script>
<script src="../../../lms/permission.js"></script>
<script src="../../../lms/config.js"></script>

<script>
    // 相关常量
    const PRIMARY_KEY = "id";
    const RESTFUL_API = lms_config.api_url + "/adminapi/wf/process";
    const SET_STATUS_API = lms_config.api_url + "/adminapi/wf/process/set_status";

    //没有登录退回登录页面
    if (!checkLogin()) {
        location.href = lms_config.base_path + "/login.html";
    }

    layui.use(['table', 'form', 'jquery', 'common', 'util', 'encrypt', 'popup'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let common = layui.common;
            let util = layui.util;
            let popup = layui.popup;
            let encrypt = layui.encrypt;

            let MODULE_PATH = "";

            let cols = [
                {
                    type: 'checkbox'
                },
                {
                    title: 'ID',
                    field: 'id',
                    align: 'center',
                    sort: true,
                    hide: true,
                    width: 60
                },

                {
                    title: '唯一编码',
                    field: 'name',
                    align: 'left',
                    width: 150
                },
                {
                    title: '显示名称',
                    field: 'display_name',
                    align: 'left'
                },
                {
                    title: '版本',
                    field: 'version',
                    align: 'left'
                },
                {
                    title: '状态',
                    field: 'state',
                    align: 'left',
                    templet: '#status',
                    width: 100
                },
                {
                    title: '创建时间',
                    field: 'create_time',
                    align: 'left',
                    width: 170
                },
                {
                    title: '更新时间',
                    field: 'update_time',
                    align: 'left',
                    width: 170
                },
                {
                    title: '操作',
                    toolbar: '#table-bar',
                    align: "left",
                    fixed: "right",
                    minWidth: 190,
                    width: 200,
                }
            ]


            window.render = function () {
                table.render({
                    elem: "#data-table",
                    url: RESTFUL_API,
                    page: true,
                    cols: [cols],
                    skin: "line",
                    toolbar: "#table-toolbar",
                    autoSort: false,
                    parseData: function (ret) {
                        return {
                            "code": ret.status === 200 ? 0 : -1, // 解析接口状态
                            "msg": ret.msg, // 解析提示文本
                            "count": ret.data.count, // 解析数据长度
                            "data": ret.data.list // 解析数据列表
                        };
                    },
                    defaultToolbar: [{
                        title: "刷新",
                        layEvent: "refresh",
                        icon: "layui-icon-refresh",
                    }, "filter", "print", "exports"]
                })
            }

            window.render();

            table.on('tool(data-table)', function (obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                } else if (obj.event == 'check') {
                    window.check(obj)
                }
            });

            table.on('toolbar(data-table)', function (obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refreshTable()
                } else if (obj.event === 'batchRemove') {
                    window.batchRemove(obj);
                }
            });

            // 头部搜索栏
            form.on('submit(table-query)', function (data) {
                table.reload("data-table", {
                    page: {
                        curr: 1
                    },
                    where: data.field
                })
                return false;
            });

            window.add = function () {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '新增流程定义',
                    shade: 0.1,
                    area: [common.isModile() ? '100%' : '800px', common.isModile() ? '100%' : '450px'],
                    content: MODULE_PATH + 'add.html?operation=add'
                });
            }

            window.edit = function (obj) {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '修改流程定义',
                    shade: 0.1,
                    area: [common.isModile() ? '100%' : '800px', common.isModile() ? '100%' : '450px'],
                    content: MODULE_PATH + 'add.html?operation=edit&' + PRIMARY_KEY + '=' + obj.data[PRIMARY_KEY]
                });
            }

            window.check = function (obj) {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '查看流程定义',
                    shade: 0.1,
                    area: [common.isModile() ? '100%' : '800px', common.isModile() ? '100%' : '450px'],
                    content: MODULE_PATH + 'check.html?operation=edit&' + PRIMARY_KEY + '=' + obj.data[PRIMARY_KEY]
                });
            }


            /**
             * 删除指定数据
             * @param obj
             */
            window.remove = function (obj) {
                layer.confirm('次操作将永久删除该记录,是否继续？', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: RESTFUL_API + '/' + obj.data.id,
                        type: 'DELETE',
                        dataType: 'json',
                        success: function (ret) {
                            layer.close(loading);
                            if (ret && ret.status == 200) {
                                popup.success(ret.msg, function () {
                                    obj.del();
                                })
                            } else {
                                popup.failure(ret.msg);
                            }
                        },
                        error: function (ret) {
                            alert("出错" + ret.status + "：" + ret.responseText);
                        }
                    })
                });
            }

            /**
             * 管理员状态
             */
            form.on('switch(process-status)', function (data) {
                let status = data.elem.checked ? 1 : 0;
                let id = this.value;
                let load = layer.load();

                layui.$.ajax({
                    url: SET_STATUS_API + '/' + id + '/' + status,
                    type: 'PUT',
                    dataType: 'json',
                    success: function (ret) {
                        layer.close(load);
                        if (ret && ret.status == 200) {
                            popup.success(ret.msg)
                        } else {
                            popup.failure(ret.msg, function () {
                                //失败回退开关
                                $(data.elem).prop('checked', !$(data.elem).prop('checked'));
                                form.render();
                            })
                        }
                    }

                })
            });


            /**
             * 刷新
             */
            window.refreshTable = function () {
                table.reloadData("data-table", {
                    scrollPos: "fixed"
                });
            }
        }
    )
</script>
</body>
</html>
